<template>
  <div class="coupon">
    <h1>优惠券</h1>
    <hr />
    <el-row class="coupon-add">
      <el-button icon="el-icon-plus" type="primary" @click="couponAdd"
        >添加优惠券</el-button
      >
    </el-row>
    <el-table :data="couponList" style="width: 100%">
      <el-table-column prop="id" label="编号" width="100"> </el-table-column>
      <el-table-column prop="name" label="优惠券名称" width="100">
      </el-table-column>
      <el-table-column prop="giveType" label="发布方式" width="100">
      </el-table-column>
      <el-table-column prop="usedRange" label="使用范围" width="100">
      </el-table-column>
      <el-table-column prop="begExp" label="开始时间" width="180">
      </el-table-column>
      <el-table-column prop="endExp" label="结束时间" width="180">
      </el-table-column>
      <el-table-column prop="price" label="价格" width="100"> </el-table-column>
      <el-table-column prop="number" label="发布数量/领取数量" width="180">
      </el-table-column>
      <el-table-column prop="inUsed" label="叠加使用" width="100">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :page-size="pageSize"
      :total="totalCurrent"
      @current-change="currentChange"
    >
    </el-pagination>
  </div>
</template>



<script>
export default {
  data() {
    return {
      couponList: [],
      currentPage: 1,
      pageSize: 10,
      totalCurrent: 0,
      checked: false
    };
  },
  created() {
    this.getInit(this.currentPage, this.pageSize);
  },
  methods: {
    
    //列表数据渲染
    getInit(currentPage, pageSize) {
      this.$http({
        url: "/coupon/selectByPage",
        method: "GET",
        params: {
          currentPage,
          pageSize,
        },
      })
        .then((res) => {
          console.log(res.data.data);

          let coupon = res.data.data.list;
          for (let i = 0; i < coupon.length; i++) {
            //使用范围处理
            switch (coupon[i].usedRange) {
              case 1:
                coupon[i].usedRange = "全商城通用";
                this.checked = true;
                break;
              case 2:
                coupon[i].usedRange = "代办理赔订单";
                this.checked = true;
                break;
              case 3:
                coupon[i].usedRange = "自助理赔订单";
                this.checked = true;
                break;
              case 4:
                coupon[i].usedRange = "代办保养订单";
                this.checked = true;
                break;
              default:
                coupon[i].usedRange = "全商城通用";
                this.checked = true;
            }

            //发布方式处理
            switch (coupon[i].giveType) {
              case 1:
                coupon[i].giveType = "系统发放";
                break;
              case 2:
                coupon[i].giveType = "公司举行活动发放";
                break;
              case 3:
                coupon[i].giveType = "第三方";
                break;
              default:
                coupon[i].giveType = "系统发放";
            }

            //叠加使用判断
            if (coupon[i].inUsed) {
              coupon[i].inUsed = "是";
            } else {
              coupon[i].inUsed = "否";
            }
          }
          this.couponList = coupon; //列表数据
          this.totalCurrent = res.data.data.total; //数据总条数
        })
        .catch((err) => {
          console.log("获取列表数据失败:", err);
        });
    },
    //切换分页重新渲染
    currentChange(currentPage) {
      console.log("当前页：", currentPage);
      this.currentPage = currentPage;
      this.getInit(currentPage, this.pageSize);
    },
    //添加
    couponAdd() {
      this.$router.push("/cpscore/coupon/add");
    },
    //修改
    handleEdit(index, row) {
      console.log(index, row);

      this.$router.push({
        path: "/cpscore/coupon/add",
        query: { row: row },
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.coupon {
  min-width: 1230px;
}

h1 {
  margin-bottom: 10px;
}

.el-row {
  margin: 10px 0;
}

.el-pagination {
  text-align: center;
  margin-top: 40px;
}
</style>